package com.gxwy.changyongzhanghu.dao;

import java.util.List;



import org.hibernate.Query;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.gxwy.changyongzhanghu.form.ChangYongZhangHu;

@Repository
public class ChangYongZhangHuDaoImpl implements ChangYongZhangHuDao {

	@Autowired	
	private SessionFactory sessionFactory;
	
	@SuppressWarnings("unchecked")
	public List<ChangYongZhangHu> findAll() {
		return sessionFactory.getCurrentSession().createQuery("from ChangYongZhangHu").list();
	}

	public ChangYongZhangHu findByShouKuanZhangHao(String skzh) {
		return (ChangYongZhangHu) sessionFactory.getCurrentSession().createQuery("from ChangYongZhangHu c where c.shoukuanzhanghao=?").setString(0, skzh).uniqueResult();
		
	}


	public void AddChangYongZhangHu(ChangYongZhangHu contact) {
		sessionFactory.getCurrentSession().save(contact);
	}

	public void Update(ChangYongZhangHu contact,Integer id) {

		String hql="Update ChangYongZhangHu As m SET m.shoujihao=?,m.shoukuanzhanghao=?, m.youxiang=?,m.zhanghubieming=?,zhanghuhuming=?,zhanghuleixing=?,zhuanruyinhang=? where m.id=?";
		
		Query query=(Query) sessionFactory.getCurrentSession().createQuery(hql);
		((org.hibernate.Query) query).setString(0,contact.getShoujihao());
		query.setString(1, contact.getShoukuanzhanghao());
		query.setString(2, contact.getYouxiang());
		query.setString(3,contact.getZhanghubieming());
		query.setString(4, contact.getZhanghuhuming());
		query.setString(5,contact.getZhanghuleixing());
		query.setString(6,contact.getZhuanruyinhang());
		query.setInteger(7, id);
		query.executeUpdate();
	}

	public void Delete(Integer id) {
		ChangYongZhangHu contact = (ChangYongZhangHu) sessionFactory.getCurrentSession().load(
				ChangYongZhangHu.class, id);
		if (null != contact) {
			sessionFactory.getCurrentSession().delete(contact);
		}
	}

	@SuppressWarnings("unchecked")
	public   List<ChangYongZhangHu>  SelectId(Integer id) {
		return sessionFactory.getCurrentSession().createQuery("from ChangYongZhangHu where id="+id).list();

	}

}
